﻿/**
* 模块名： viewModel
* 程序名: DayReport.js
**/
function viewModel() {
    var self = this;
    //this.form = { Cxrq: ko.observable(), Info_Dwmc: ko.observable() };
    this.dataSource = data.dataSource;
    this.form = ko.mapping.fromJS(data.form);
    delete this.form.__ko_mapping__;
    this.grid = {
        size: { w: 4, h: 78 },
        url: '/api/pacm/DayReport/' + ReportID,
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false
    };
    //刷新
    this.grid.queryParams(data.form);

    this.refreshClick = function () {
        window.location.reload();
    };
    //添加一个日报表（弹出层）
    this.addClick = function () {
        com.dialog({
            title: "增加报表",
            height: 240,
            width: 310,
            html: "#type-template",
            viewModel: function (w) {
                var that = this;
                this.form = {
                    txtName: ko.observable(ReportName),
                    txtYear: ko.observable(CurYear),
                    txtMonth: ko.observable(CurMonth),
                    txtDay: ko.observable(1)
                };
                this.confirmClick = function () {
                    if (this.form.txtYear() == "" || this.form.txtYear == undefined) {
                        com.message('warning', '信息不完整！-年份不能为空');
                        return;
                    }
                    if (this.form.txtMonth() == "" || this.form.txtMonth() == undefined) {
                        com.message('warning', '信息不完整！-月份不能为空');
                        return;
                    }
                    if (this.form.txtDay() == "" || this.form.txtDay() == undefined) {
                        com.message('warning', '信息不完整！-天次不能为空');
                        return;
                    }
                    $.ajax({
                        url: "/Pacm/DayReport/AddInfo",
                        type: 'Post',
                        data: { reportID: ReportID, year: this.form.txtYear(), month: this.form.txtMonth(), day: this.form.txtDay() },
                        success: function (data) {
                            if (data == 1) {
                                com.message('success', '添加成功！');
                                w.dialog('close');
                                self.searchClick();
                            } else if (data == 3) {
                                com.message('warning', '该天的报表已存在！');
                            } else {
                                com.message('warning', '添加失败！');
                            }
                        }

                    });
                };
                this.cancelClick = function () {
                    w.dialog('close');
                };
            }
        });
    };
    //编辑一个日报表（打开tab页）
    this.editClick = function () {
        self.showEditClick();
    };
    //打开对应的日报表
    this.showEditClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', '请选择之后进行操作！');
        com.openTab("报表编辑", "/pacm/DayReport/EditReport/" + row["Info_Id"] + "?v=" + Math.random());
    };
    //删除一条日报计量
    this.deleteClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', "请选择要操作的数据!");
        com.message('confirm', "确定要删除该数据吗?", function (b) {
            if (b) {
                com.ajax({
                    type: 'DELETE',
                    url: "/api/pacm/DayReport/Delete/" + row["Info_Id"],
                    success: function () {
                        com.message('success', "删除成功!");
                        self.searchClick();
                    }
                });
            }
        });
    };
    //搜索
    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    }
    //清除条件
    this.clearClick = function () {
        $.each(self.form, function () { this(''); });
        this.searchClick();
    }
    this.auditClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', "请选择数据!");
        com.auditDialog(function (d) {
            com.ajax({
                type: 'POST',
                url: "/api/pacm/DayReport/audit/" + row["Info_Id"],
                data: JSON.stringify(d),
                success: function () {
                    com.message('success', "操作成功!");
                }
            });
        });
    };
    //datagrid双击事件
    this.grid.onDblClickRow = this.showEditClick;
}
